package com.jason.platform.supervision.task;

import android.util.Log;
import com.google.gson.GsonBuilder;
import com.jason.platform.supervision.beans.LoginResp;
import org.kymjs.aframe.http.HttpCallBack;
import org.kymjs.aframe.http.KJHttp;
import org.kymjs.aframe.utils.CodecUtils;

/**
 * @ClassName: LoginTask
 * @Description: 登录
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-28 14:30:55
 */
public class LoginTask extends Task {

    private static final String TAG = LoginTask.class.getSimpleName();

    public LoginTask(TaskListener listener){
        super(listener);
    }


    @Override
    public void execute() {
        Log.i(TAG, "post Params:" + mParams.getParamString());
        mKJHttp.post(HttpConfig.URL_API_HOST + HttpConfig.POST_URL_DATB_LOGIN, mParams, new HttpCallBack() {
            @Override
            public void onLoading(long count, long current) {

            }

            @Override
            public void onSuccess(Object arg0) {
                String result = null;
                try {
                    result = CodecUtils.base64Decode(arg0.toString());
                    Log.i(TAG,"post Result:" +  result);
                    LoginResp loginResp = new GsonBuilder().create().fromJson(
                            result, LoginResp.class);
                    if (loginResp != null){
                        isSuccess = loginResp.isSuccess();
                        rspDesc = loginResp.getRespMsg();
                        if (loginResp.isSuccess()){
                            if (!loginResp.isEmpty()){
                                resData = loginResp.getValues().get(0);
                            }
                        }
                    }
                }catch (Exception e){
                    isSuccess = false;
                }
                listener.onTaskResult(LoginTask.this);
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                Log.i(TAG, "stateCode:"+ errorNo + ",errorMessage" + strMsg);
                isSuccess = false;
                listener.onTaskResult(LoginTask.this);
            }
        });

    }
}
